package com.hrp.service;

import com.hrp.domain.ResponseData;
import com.hrp.domain.UimmsPrice;
import com.hrp.domain.UimmsReceiveMoney;
import com.hrp.hrpdao.PriceSettingDao;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/5/6 0006.
 */
@Service
public class PriceSettingService {


    private static final Logger logger = LoggerFactory.getLogger(PriceSettingService.class);

    @Autowired
    private PriceSettingDao priceSettingDao;


    public ResponseData savePriceSetting(UimmsPrice uimmsPrice) {

        try{
            UimmsPrice uimmsPriceOld = priceSettingDao.getPriceSettingById(uimmsPrice.getId());
            if(uimmsPriceOld==null){
                priceSettingDao.savePriceSetting(uimmsPrice);
            }else{
                priceSettingDao.updatePriceSetting(uimmsPrice);
            }
            return ResponseData.build(200,"保存成功");
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.build(500,"保存失败");
        }

    }

    public Map<String,Object> getPriceSettingByPage(Integer page, Integer rows) {



        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
        try{
            Integer total = priceSettingDao.getTotalCount();
            //List<Jxkhzq> list = jxkhzqDao.getJxkhzqByPage(10,20);

            Integer startIndex = (page-1)*rows;
            Integer endIndex = page*rows;
            List<Integer> listStr = new ArrayList<>();
            for(Integer i=(startIndex+1);i<=endIndex;i++){
                listStr.add(i);
            }
            List<UimmsPrice> list = priceSettingDao.getPriceSettingByPage(listStr);
            jsonMap.put("total", total);//total键 存放总记录数，必须的
            jsonMap.put("rows", list);//rows键 存放每页记录 list
            return jsonMap;
        }catch (Exception e) {
            e.printStackTrace();
            logger.info("获取分页账期数据失败");
            return jsonMap;

        }
    }

    public ResponseData batchUpdatePrice(Integer id) {
        try{
            priceSettingDao.batchUpdatePrice(id);
            return ResponseData.build(200,"更新成功");
        }catch (Exception e) {
            e.printStackTrace();
            logger.info("更新成功");

            return ResponseData.build(500,"更新失败");
        }
    }
}
